package com.lagou.seckill.mvc.service;

import com.lagou.seckill.constant.StatusEnum;
import com.lagou.seckill.dto.Response;
import com.lagou.seckill.util.OrderUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

@Slf4j
@Service
@RequiredArgsConstructor
public final class PayService {

    final RedisTemplate redisTemplate;

    public Response pay(String commodityId) {
        String order = OrderUtil.generateOrder(commodityId);
        Assert.isTrue(redisTemplate.hasKey(order), "订单不存在!");
        Object orderStatus = redisTemplate.opsForValue().get(order);
        Assert.isTrue(StatusEnum.TOBE_PAY.code.equals(orderStatus), "订单状态不正确!");
        redisTemplate.opsForValue().set(order, StatusEnum.PAYED.code);
        log.info("订单[{}]支付成功", order);
        return Response.OK();
    }
}
